const {comMysql} = require("../db/mysql")
const xss = require("xss")

const addRecomment = (req,res)=>{
  
  let {username,content,blogId,realname,commentId,avatar} = req.body
  content = xss(content)
  let time = new Date().getTime()
  let sql = `insert into commentback(username,content,time,goodNum,realname,commentid,blogid,avatar) values(?,?,?,?,?,?,?,?)`
  let arr = [username,content,time,0,realname,commentId,blogId,avatar]
  return comMysql(sql,arr)
}

const delRecomment = (req,res)=>{
  let {id,username} = req.body
  let sql = `delete from commentback where id =? and username = ?`
  let arr = [id,username]
  return comMysql(sql,arr)
}

const updateRecomment = (req,res)=>{
  let {id,goodNum} = req.body
  let sql = `update commentback set goodNum = ? where id = ?`
  let arr = [goodNum,id]
  console.log(arr);
  console.log(sql);
  
  return comMysql(sql,arr)
}

const searchRecomment = (req,res)=>{
  let {username,goodNum,time,blogId,commentId} = req.query
  let sql = `SELECT commentback.commentid,commentback.content,commentback.goodNum,commentback.id,commentback.time,users.avatar,commentback.realname 
  from commentback LEFT JOIN users on commentback.username=users.username where 1=1 `
  if(username){
    sql+=`and username = ${username} `
  }
  if(blogId){
    sql+=`and blogId = ${blogId} `
  }
  if(commentId){
    sql+=`and commentId = ${commentId} `
  }
  if(goodNum){
    sql+=`order by goodNum desc`
  }

  if(time){
    sql+=`order by time desc`
  }
  return comMysql(sql)
}

module.exports={
  addRecomment,
  delRecomment,
  searchRecomment,
  updateRecomment
}